<?php
class base{
	protected $sys=array('root'=>'','app'=>'','lib'=>'','dat'=>'','etc'=>'','che'=>'');
	protected $itm=array('model'=>'','view'=>'','zone'=>'Asia/Shanghai','char'=>'utf8','environment'=>E_ERROR);
	protected $cache=array('time'=>60,'suffix'=>'.cache');
	protected $lib=array();
	protected $tpl=array('tpl'=>'','strip'=>false,'buffer'=>'','caching'=>false,'vars'=>array());
	protected $app=array();
	protected $classes=array();
	public    $runtime=0;
	public function __construct($etc){
		$this->sys['root']=rtrim(dirname(__FILE__),'lib');
		$this->sys['app']=$this->sys['root'].'app/';
		$this->sys['lib']=$this->sys['root'].'lib/';
		$this->sys['dat']=$this->sys['root'].'dat/';
		$this->sys['etc']=$this->sys['root'].'etc/';
		$this->sys['che']=$this->sys['root'].'dat/che/';
		$etc=$this->config($etc);
		$array=array_filter($etc,'is_array');
		foreach($array as $key=>$value){$this->$key=array_merge($this->$key,$value);}
		$string=array_filter($etc,'is_string');
		foreach($string as $key=>$value){define($key,$value);}
		$this->itm['model']=$this->sys['root'].$this->itm['model'];
		$this->itm['view']=$this->sys['root'].$this->itm['view'];
		$this->tpl['tpl']=$this->itm['view'];
		error_reporting($this->itm['environment']);
		date_default_timezone_set($this->itm['zone']);
		$this->runtime=$this->server('REQUEST_TIME');
	}
	public function vars($var,$item,$index=false){
		$pref=false;$var=$this->$var;
		if(isset($var)){
			if($index==false){
				if(isset($var[$item])){
					$pref=$var[$item];
				}
			}else{
				if(isset($var[$item])&&isset($var[$item][$index])){
					$pref=$var[$item][$index];
				}
			}
		}
		return $pref;
	}
	public function config($etc){
		$config=array();
		if(is_string($etc)){$etc=array($etc);}
		foreach($etc as $value){
			$file=$this->sys['etc'].$value.'.php';
			if(file_exists($file)){
				require($file);
			}else{
				$this->error('config is error.');
			}
		}
		return $config;
	}
	public function cookie($name,$value=false,$expire=0,$path='/',$domain='',$secure=false){
		if(is_bool($value)){
			return $this->input($_COOKIE,$name,$value);
		}else{
			return setcookie($name,$value,$expire,$path,$domain,$secure);
		}
	}
	private function input(&$source,&$index,&$security){
		$var=false;
		if(is_string($index)){
			$var=isset($source[$index])?$source[$index]:false;
			if($security&&$var){$var=$this->escape($var);}
		}else if(is_array($index)){
			foreach($index as $key=>$value){
				$index[$key]=isset($source[$key])?$source[$key]:false;
				if($value&&$index[$key]){$index[$key]=$this->escape($index[$key]);}
			}
			$var=$index;
		}
		return $var;
	}
    public function escape($var,$res=null){
		if(function_exists('mysql_real_escape_string')&&is_resource($res)){
			$var=is_string($var)?mysql_real_escape_string($var,$res):$var;
		}else if(function_exists('mysql_escape_string')){
			$var=is_string($var)?mysql_escape_string($var):$var;
		}else{
			$var=is_string($var)?addslashes($var):$var;
		}
		return $var;
    }
	public function view($view,$params=array()){
		$params=array_merge(array('tag'=>'','strip'=>$this->tpl['strip'],'caching'=>$this->tpl['caching'],'time'=>$this->cache['time'],'suffix'=>$this->cache['suffix']),$params);
		$view=$this->tpl['tpl'].$view;
		if(file_exists($view)){
			extract($this->tpl['vars']);$this->tpl['vars']=array();
			ob_start();
			if((bool)@ini_get('short_open_tag')===false){
				eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ',file_get_contents($view))));
			}else{
				include($view);
			}
			$this->tpl['buffer']=ob_get_contents();
			ob_end_clean();
			if($params['strip']){$this->tpl['buffer']=preg_replace('![\t ]*[\r\n]+[\t ]*!','',$this->tpl['buffer']);}
			if($params['caching']){
				$this->cache(array('tag'=>$params['tag'],'time'=>$params['time'],'content'=>$this->tpl['buffer'],'suffix'=>$params['suffix'],'path'=>'html'));
			}
		}else{
			$this->error('view('.$view.') is not found!');
		}
		return true;
	}
	public function fetch($value=false){
		if($value!==false){
			$temp=$this->tpl['buffer'];
			$this->tpl['buffer']=$value;
			return $temp;
		}else{
			return $this->tpl['buffer'];
		}
	}
	public function set($vars=array(),$val=''){
		if($val!=''&&is_string($vars)){
			$vars=array($vars=>$val);
		}else if(is_object($vars)){
			$vars=get_object_vars($vars);
		}
		if(is_array($vars)){
			$this->tpl['vars']=array_merge($this->tpl['vars'],$vars);
		}
	}
	public function cache($params){
		$params=array_merge(array('tag'=>'','content'=>'','path'=>'','time'=>$this->cache['time'],'suffix'=>$this->cache['suffix']),$params);
		$params['path']=$this->sys['che'].$params['path'].'/';
		if(!file_exists($params['path'])){
			if(!mkdir($params['path'])){
				$this->error('cache file Can\'t write!');
			}
		}
		$params['tag']=md5($params['tag']);
		if(empty($params['content'])){
			if(file_exists($params['path'].$params['tag'].$params['suffix'])){
			   if(($this->runtime-filemtime($params['path'].$params['tag'].$params['suffix']))<$params['time']){
			  	 	return file_get_contents($params['path'].$params['tag'].$params['suffix']);
			   }else{
			      	return false;
			   }
			}else{
				return false;
			}
		}else{
			$old=umask(0);$fp=fopen($params['path'].$params['tag'].$params['suffix'],'w');
			if($fp){
			   @fwrite($fp,$params['content']);@fclose($fp);@umask($old);
			   return true;
			}else{
			   $this->error('cache file Can\'t write!');
			}
		}
	}
	public function &model($class,$params=array()){
		return $this->load($this->itm['model'].$class.'/',$class,$params);
	}
	public function &library($class,$params=array()){
		return $this->load($this->sys['lib'].$class.'/',$class,$params);
	}
	protected function &load($target,$class,$params){
		if(!is_array($params)){$this->error('params is error!');}
		$key=md5($target);
		if(isset($this->classes[$key])){
			return $this->classes[$key];
		}
		if(file_exists($target.$class.'.class.php')){
			require($target.$class.'.class.php');
			$this->classes[$key] = new $class();
			$this->classes[$key]->yum=&$this;
			if(method_exists($this->classes[$key],'init')){
				if(isset($this->lib[$class])){
					$this->lib[$class]=empty($this->lib[$class])?array():$this->lib[$class];
					$params=array_merge($this->lib[$class],$params);
				}
				$this->classes[$key]->init($params);
			}
			return $this->classes[$key];
		}else{
			$this->error($target.$class.'.class.php not found!');
		}
	}
	public function error($msg){
		echo '<font color="red">'.$msg.'</font><br/>';
		exit(0);
	}
	public function __call($name,$pams){
		$name=strtoupper($name);
		if(in_array($name,array('GET','POST','SERVER','ENV','SEEION','REQUEST','FILES'))){//argv
			$source=array();eval('$source=$_'.$name.';');$pams[1]=isset($pams[1])?$pams[1]:false;
			return $this->input($source,$pams[0],$pams[1]);
		}
	}
	public function __destruct(){
		echo $this->tpl['buffer'];
	}
}
?>